<?php
/**
 * Created on 2010-4-28
 *
 * @author Niap <zpr9527@163.com>
 *
 */
class File {
 private $Fp;
 private $Pipe; //(fopen,popen)(f,p)
 private $File;
 private $OpenMode; //(r,r+,w,w+,a,a+,b)
 private $Data;

 function File($File,$Mode = 'r',$Data4Write='',$Pipe = 'f'){
			  $this -> File = $File;
       $this -> Pipe = $Pipe;
        if($Mode == 'dr'){
             $this -> OpenMode = 'r';
            $this -> OpenFile();
              $this -> getFileData();
        }else{
            $this -> OpenMode = $Mode;
             $this -> OpenFile();
         }
         if($this->OpenMode=='w'&$Data4Write!=''){
               $this -> WriteFile($Data4Write,$Mode = 3);
         }
     }

     function OpenFile(){
     if ($this -> OpenMode == 'r'||$this -> OpenMode == 'r+'){
            if($this->CheckFile()){
                if ($this -> Pipe == 'f') {
                     $this->Fp = fopen($this -> File, $this -> OpenMode);
                 } elseif ($Pipe == 'p') {
                     $this->Fp = popen($this -> File, $this -> OpenMode);
                 }else{
                     Die(" 请检查文件打开参数3,f:fopen()");
                 }
             } else {
                 Die("文件访问错误,请检查文件是否存在!");
             }
           } else {
             if ($this -> Pipe == 'f') {
                 $this->Fp = fopen($this -> File, $this -> OpenMode);
             } elseif ($Pipe == 'p') {
                 $this->Fp = popen($this -> File, $this -> OpenMode);
             } else {
                 Die("请检查文件打开参数3,f:fopen()");
             }
         }
     }


     function CloseFile(){
          if ($this->Pipe == 'f'){
            @fclose($this->Fp);
        } else {
             @pclose($this->Fp);
         }
     }

     function getFileData(){
         @flock($this->Fp, 1);
      $Content = fread($this->Fp, filesize($this->File));
         $this->Data = $Content;
     }


     function CheckFile(){
         if (file_exists($this -> File)) { return true; } else { return false; }
      }

     function WriteFile($Data4Write,$Mode = 3){
         @flock($this->Fp,$Mode);
         fwrite($this->Fp,$Data4Write);
         $this->CloseFile();
         return true;
     }

 }
?>
